<?php
	require_once dirname(BASEPATH) . '/application/models/local/data/_init_model.php';
	require_once dirname(BASEPATH) . '/application/libraries/define/Directory_Path.php';
	
	class mod_article extends _init_model
	{
		public $ART_ID;
		public $ART_CODE;
		public $ART_TITLE;
		public $ART_IMG_THUMB;
		public $ART_TEXT;
		public $ART_DATE;
		public $ART_TIME;
		public $ART_CAT_ID;
		public $ART_TAG;
		public $ART_FEATURE;
		public $ART_PUBLISHED;
		public $ADMIN_ID;
		
		function __construct(){
			parent::__construct();
		
			$this->tableName = "article";
			$this->ART_PUBLISHED = true;
		}						
		
		function get_article_list($display_length=null, $start_index=null, $fields=null){
			if (!is_null($fields) && is_array($fields))
				$this->db->select($fields);
						
			$this->db->join('article_category', 'article_category.ART_CAT_ID = article.ART_CAT_ID');
			$this->db->join('admin', 'admin.ADM_ID = article.ADM_ID');
			$this->db->order_by('ART_ID', 'DESC');					
					
			$query = $this->db->get($this->tableName, $display_length, $start_index);		
			
			$return = $query->result_array();
			if (count($return) > 0){
				$this->load->library('define/directory_path');
				for ($i=0; $i<count($return); $i++){
					$return[$i]['ART_IMG_THUMB'] = $this->directory_path->img_article_thumb_path . $return[$i]['ART_IMG_THUMB'];
				}
			}
				
			return $return;
		}
		function get_single_article($article_code, $fields=null){
			if (!is_null($fields))
				$this->db->select($fields);
			else
				$this->db->select(array('article.*', 'admin.ADM_NAME', 'admin.ADM_IMG', 'article_category.ART_CAT_NAME'));
				
			$this->db->where('ART_CODE', $article_code);			
			$this->db->join('admin', 'admin.ADM_ID = article.ADM_ID', 'left');
			$this->db->join('article_category', 'article_category.ART_CAT_ID = article.ART_CAT_ID');
			$query = $this->db->get($this->tableName);
			
			if ($query->num_rows() > 0){
				$row = $query->row_array(0);
				
				if (isset($row['ADM_NAME'])){
					if (empty($row['ADM_NAME']))
						$row['ADM_NAME'] = 'Unknown';
				}
					
				$this->load->library('define/directory_path');
				if (isset($row['ADM_IMG'])){
					if (!empty($row['ADM_IMG']))					
						$row['ADM_IMG'] = $this->directory_path->img_avatar_staff . $row['ADM_IMG'];
					else
						$row['ADM_IMG'] = $this->directory_path->img_no_avatar;
				}
					
				if (isset($row['ART_IMG_THUMB'])){
					if (!empty($row['ART_IMG_THUMB']))
						$row['ART_IMG_THUMB'] = $this->directory_path->img_upload_article_thumb_path . $row['ART_IMG_THUMB'];								
				}
					
				return $row;
			}
			else
				return null;
		}
		function get_feature_article($display_length=null, $start_index=null){
			$this->db->where('article.ART_FEATURE', true);
			$this->db->where('article.ART_PUBLISHED',true);
			$data = $this->get_article_list($display_length, $start_index);
			
			return $data;
		}
	
		function generate_article_code($category){
			return date('Y') . date('m') . date('d') . str_pad($category, 1, '0', STR_PAD_LEFT) . date('H') . date('i') . date('s');
		}	
						
		function get_article_title_by_code($article_code){
			$this->db->select('ART_TITLE');
			$this->db->where('ART_CODE', $article_code);
			$query = $this->db->get($this->tableName);
			
			if ($query->num_rows() > 0)
				return $query->row(0)->ART_TITLE;
			else
				return null;
		}
		function get_article_title_by_tag($tagname, $display_length=null, $start_index=0, $shuffle=true){
			$this->db->select(array('article.ART_CODE', 'article.ART_TITLE', 'article.ART_IMG_THUMB'));
			
			if (is_array($tagname)){
				for ($i=0; $i<count($tagname); $i++){
					if ($i<=0)	
						$this->db->like('article.ART_TAG', $tagname[$i]);
					else
						$this->db->or_like('article.ART_TAG', $tagname[$i]);
				}
			}
			if ($shuffle)
				$this->db->order_by('ART_ID', 'random');
			$query = $this->db->get($this->tableName, $display_length, $start_index);
			$return = $query->result_array();
			
			if (count($return) > 0){
				$this->load->library('define/directory_path');
				for ($i=0; $i<count($return); $i++)
					$return[$i]['ART_IMG_THUMB'] = $this->directory_path->img_article_thumb_path . $return[$i]['ART_IMG_THUMB'];
			}
			return $return;			
		}
		function get_article_title_by_category($category_id, $display_length=null, $start_index=0, $shuffle=true){
			$this->db->select(array('article.ART_CODE', 'article.ART_TITLE', 'article.ART_IMG_THUMB'));
			$this->db->like('article.ART_CAT_ID', $category_id);
			if ($shuffle)
				$this->db->order_by('ART_ID', 'random');
			$query = $this->db->get($this->tableName, $display_length, $start_index);
			$return = $query->result_array();
			
			if (count($return) > 0){
				$this->load->library('define/directory_path');
				for ($i=0; $i<count($return); $i++)
					$return[$i]['ART_IMG_THUMB'] = $this->directory_path->img_article_thumb_path . $return[$i]['ART_IMG_THUMB'];
			}
			return $return;	
		}
		
		function get_article_by_tag($tagname, $display_length=null, $start_index=0){
			$this->db->like('article.ART_TAG', $tagname);
			return $this->get_article_list($display_length, $start_index);			
			/*$sqlcmd = 'SELECT ART_ID FROM ' . $this->tableName;			
			if (!is_array($tagname))
				$sqlcmd .= ' WHERE ART_TAG LIKE "%' . $tagname . '%"';
			else {
				for ($i=0; $i<count($tagname); $i++){
					if ($i <= 0)
						$sqlcmd .= ' WHERE ART_TAG LIKE "%' . $tagname[$i] . '%"';
					else
						$sqlcmd .= ' OR ART_TAG LIKE "%' . $tagname[$i] . '%"';
				}	
			}
			
			$sqlcmd .= ' ORDER BY ART_ID DESC';		
			$sqlcmd .= ' LIMIT ' . $display_length . ',' . $start_index;			
			$query = $this->db->query($sqlcmd);			
			$result_id_list = $query->result_array();
			
			$return = array();
			if (count($result_id_list) > 0){
				foreach ($result_id_list as $row){
					if (!is_null($fields))
						$this->db->select($fields);
					$this->db->where('ART_ID', $row['ART_ID']);
					$query = $this->db->get($this->tableName);
					
					if ($query->num_rows() > 0)
						array_push($return, $query->row_array(0));
				}
			}
			
			if (count($return) > 0){
				$this->load->library('define/directory_path');
				for ($i=0; $i<count($return); $i++){
					if (!empty($return[$i]['ART_IMG_THUMB']))
						$return[$i]['ART_IMG_THUMB'] = $this->directory_path->img_article_thumb_path . $return[$i]['ART_IMG_THUMB'];
					else
						$return[$i]['ART_IMG_THUMB'] = $this->directory_path->img_no_thumb;
				}
			}
			
			return $return;*/
		}
		function get_article_by_category_name($category_name, $display_length=null, $start_index=0){
			if (!empty($category_name)){
				$this->load->model('local/data/article/mod_article_category');
				$category_id = $this->mod_article_category->get_category_id_by_name(urldecode($category_name));
				
				$this->db->where('article.ART_CAT_ID', $category_id);
			}
			return $this->get_article_list($display_length, $start_index);			
		}
		function get_article_count_by_tag($tagname){				
			$this->db->like('article.ART_TAG', $tagname, 'both');			
			return $this->db->count_all_results($this->tableName);
		}
		function get_article_count_by_category_name($category_name){
			if (!empty($category_name)){
				$this->load->model('local/data/article/mod_article_category');
				$category_id = $this->mod_article_category->get_category_id_by_name(urldecode($category_name));
				
				$this->db->where('article.ART_CAT_ID', $category_id);
			}
			return $this->db->count_all_results($this->tableName);
		}
		function get_tag($article_code){
			$this->db->select('ART_TAG');
			$this->db->where('ART_CODE', $article_code);
			$query = $this->db->get($this->tableName);
			
			if ($query->num_rows() > 0)				
				return explode(',', $query->row(0)->ART_TAG);			
			else
				return array();
		}
			
		function set_feature($article_code, $state=true){
			$this->db->where('ART_CODE', $article_code);
			return $this->db->update($this->tableName, array('ART_FEATURE' => $state));
		}
		function set_published($article_code, $state=true){
			print $article_code;			
			$this->db->where('ART_CODE', $article_code);
			return $this->db->update($this->tableName, array('ART_PUBLISHED' => $state));
		}	
						
		function add(){
			$this->load->library('custom_datetime');			
			
			$this->ART_CODE = $this->generate_article_code($this->ART_CAT_ID);
			$this->ART_DATE = $this->custom_datetime->current_date();
			$this->ART_TIME = $this->custom_datetime->current_time();
			
			// add tag name
			if (!empty($this->ART_TAG)){
				$tag_arr = explode(',', $this->ART_TAG);
				
				for ($i=0; $i<count($tag_arr); $i++){
					$this->load->model('local/data/article/mod_tag');
					$this->mod_tag->add_tag($tag_arr[$i]);
				}
			}
			
			return $this->db->insert($this->tableName, array('ART_CODE' => $this->ART_CODE,
																'ART_DATE' => $this->ART_DATE,
																'ART_TIME' => $this->ART_TIME,
																'ART_TITLE' => $this->ART_TITLE,
																'ART_IMG_THUMB' => $this->ART_IMG_THUMB,
																'ART_TEXT' => $this->ART_TEXT,																
																'ART_CAT_ID' => $this->ART_CAT_ID,
																'ART_TAG' => $this->ART_TAG,
																'ART_FEATURE' => $this->ART_FEATURE,
																'ART_PUBLISHED' => $this->ART_PUBLISHED,
																'ADM_ID' => $this->ADM_ID));
									
		} 
		function edit($article_code){
									
			$data = array();
			if (!empty($this->ART_TITLE))
				$data['ART_TITLE'] = $this->ART_TITLE;
			if (!empty($this->ART_IMG_THUMB)){
				$data['ART_IMG_THUMB'] = $this->ART_IMG_THUMB;
				
				// remove old thumbnail
				$this->db->select('ART_THUMB_IMG');
				$this->db->where('ART_CODE', $article_code);
				$query = $this->db->query($this->tableName);
				if ($query->num_rows() > 0){
					$this->load->library('define/directory_path');
					unlink($this->directory_path->img_upload_article_thumb_path . $query->row(0)->ART_THUMB_IMG);
				}
				$this->db->flush_cache();
			}
			if (!empty($this->ART_TEXT))
				$data['ART_TEXT'] = $this->ART_TEXT;
			if (!empty($this->ART_CAT_ID))
				$data['ART_CAT_ID'] = $this->ART_CAT_ID;
			if (!empty($this->ART_TAG)){
				$data['ART_TAG'] = $this->ART_TAG;
								
				// append new tag				
				$tag_arr = explode(',', $this->ART_TAG);
				
				for ($i=0; $i<count($tag_arr); $i++){
					$this->load->model('local/data/article/mod_tag');
					$this->mod_tag->add_tag($tag_arr[$i]);
				}
			}
			if (!empty($this->ART_FEATURE))
				$data['ART_FEATURE'] = $this->ART_FEATURE;
			if (!empty($this->ART_PUBLISHED))
				$data['ART_PUBLISHED'] = $this->ART_PUBLISHED;
			
			$this->db->where('ART_CODE', $article_code);
			return $this->db->update($this->tableName, $data);
		}
		function remove($article_code){
			$row = $this->get_single_article($article_code, array('ART_IMG_THUMB'));
			
			if (file_exists($row['ART_IMG_THUMB']))
				unlink($row['ART_IMG_THUMB']);
						
			$this->db->flush_cache();
			$this->db->where('ART_CODE', $article_code);
			return $this->db->delete($this->tableName);
		}
}
?>